package com.rainszj.controller.site;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rainszj.pojo.po.Tag;
import com.rainszj.pojo.vo.IndexBlog;
import com.rainszj.service.BlogService;
import com.rainszj.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import static com.rainszj.constant.WebConstant.*;
import java.util.List;

/**
 * 前台标签页
 *
 * @author rainszj
 * 2020/3/17
 */
@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    /**
     * 展示标签下的所有博客
     *
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("/tags.html")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageInfo<Tag> pageInfo = PageHelper.startPage(pageNum, PAGE_SIZE).doSelectPageInfo(() -> blogService.listIndexBlog());
        List<Tag> tags = tagService.listAllTag();

        model.addAttribute("tags", tags);
        model.addAttribute("pageInfo", pageInfo);
        return "site/tags";
    }

    /**
     * 根据标签 Id，展示该标签下的所有博客
     *
     * @param pageNum
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/tag/{id}")
    public String queryTagList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @PathVariable("id") Long id, Model model) {
        PageHelper.startPage(pageNum, PAGE_SIZE);
        // 与博客连接的分类
        List<Tag> tags = tagService.listAllTag();
        // 根据分类id查询博客
        PageInfo<IndexBlog> pageInfo = new PageInfo<IndexBlog>(blogService.getIndexBlogByTagId(id));
        model.addAttribute("tags", tags);
        model.addAttribute("pageInfo", pageInfo);
        return "site/tags";
    }


}
